//
//  DoubleBallViewController.m
//  inrtest
//
//  Created by 宋辉 on 2017/4/21.
//  Copyright © 2017年 . All rights reserved.
//

#import "DoubleBallViewController.h"
#import "DoubleView.h"
#import "MyHeaderView.h"
#import "CollectionViewCell.h"
#import "BlueCollectionViewCell.h"

#import <AVFoundation/AVFoundation.h>

#import "BuyLotteryViewController.h"
@interface DoubleBallViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,DoubleViewDelegate>

@property(nonatomic,strong)DoubleView *Dview;
@property(nonatomic,strong)NSMutableArray *SourceArray;

@property(nonatomic,strong)NSMutableArray *R_Array;
@property(nonatomic,strong)NSMutableArray *B_Array;
@property(nonatomic,strong)NSMutableArray *BSource_Array;
@property(nonatomic,strong)NSMutableArray *Data_Array;

@end
static NSString *collectionCellIndentider = @"collectionCellIndentider";

@implementation DoubleBallViewController{
    NSString *documentPath;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
     [self initNav];
     [self initUI];
     [self CreateSource];
    
}

-(void)initNav{
    
    [self.navigationItem setTitle:@"摇一摇"];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    
}

-(void)initUI{
    self.Dview = [DoubleView new];
//                  initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeigth - 64)];
    // @"请至少选择 6 个红球"
    //    self.Dview.textlabel2.text = _R_count_text;
    self.Dview.BallCollectionView.delegate = self;
    self.Dview.BallCollectionView.dataSource = self;
    self.Dview.delegate = self;
    [self.Dview.BallCollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"coll_cell"];
    
    [self.Dview.BallCollectionView registerClass:[BlueCollectionViewCell class] forCellWithReuseIdentifier:@"B_cell"];
    
    [self.Dview.BallCollectionView registerClass:[MyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
    
    [self.view addSubview:self.Dview];
    
    
    if(iPhoneX){
        
        [self.Dview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(DEF_DEVICE_WIDTH);
            make.height.mas_equalTo(DEF_DEVICE_HEIGHT-88-34);
            make.top.equalTo(self.view).with.offset(0);
            make.leading.equalTo(self.view).with.offset(0);
        }];
        
        
    }else{
        
        [self.Dview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(DEF_DEVICE_WIDTH);
            make.height.mas_equalTo(DEF_DEVICE_HEIGHT-64);
            make.top.equalTo(self.view).with.offset(0);
            make.leading.equalTo(self.view).with.offset(0);
        }];
        
    }
    
    [self.Dview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH);
        make.height.mas_equalTo(DEF_DEVICE_HEIGHT-64);
        make.top.equalTo(self.view).with.offset(0);
        make.leading.equalTo(self.view).with.offset(0);
    }];
    
    // 设置允许摇一摇功能
    [UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
    // 并让自己成为第一响应者
    [self becomeFirstResponder];
    
}
-(void)sureButClick{
    
    if (UserInfoShare.isLogin==YES) {
        NSLog(@"---%@",_R_Array);
        NSLog(@"===%@",_B_Array);
    
        if ([_Navtitle isEqualToString:@"双 色 球"]) {
            
            if (_R_Array.count!=6) {
                
                [MXUtility showTips:@"必须选择6个红球"];
                
            }else if(_B_Array.count!=1) {
                
                [MXUtility showTips:@"必须选择1个蓝球"];
                
            }else{
                
                BuyLotteryViewController* vc=[[BuyLotteryViewController alloc]init];
                vc.R_Array=_R_Array;
                vc.B_Array=_B_Array;
                vc.titleStr=_Navtitle;
                vc.wanfaStr=@"6个红球1个蓝球";
                vc.lotteryType=@"ssq";
                [self.navigationController pushViewController:vc animated:YES];
                
            }
        }else if ([_Navtitle isEqualToString:@"超级大乐透"]){
            
            if (_R_Array.count!=5) {
                
                [MXUtility showTips:@"必须选择5个红球"];
                
            }else if(_B_Array.count!=2) {
                
                [MXUtility showTips:@"必须选择2个蓝球"];
                
            }else{
                
                BuyLotteryViewController* vc=[[BuyLotteryViewController alloc]init];
                vc.R_Array=_R_Array;
                vc.B_Array=_B_Array;
                vc.titleStr=_Navtitle;
                vc.wanfaStr=@"5个红球2个蓝球";
                vc.lotteryType=@"dlt";
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            
        }
        
    }else{
        
        [MXUtility showTips:@"请先前去登录"];
        
    }
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"开始摇动");
    
    return;
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"取消摇动");
    return;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.subtype == UIEventSubtypeMotionShake) { // 判断是否是摇动结束
        NSLog(@"摇动结束");
        [self shackButClick];
        [self shakeshake];
        
    }
    return;
}

- (void)shakeshake {
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

-(void)CreateSource{
    
    _SourceArray = [NSMutableArray arrayWithCapacity:_R_count];
    _BSource_Array  = [NSMutableArray arrayWithCapacity:_B_count];
    for (int i = 0; i < _R_count; i ++) {
        if (i < _B_count) {
            [_BSource_Array addObject:[NSString stringWithFormat:@"%d",i + 1]];
        }
        [_SourceArray addObject:[NSString stringWithFormat:@"%d",i + 1]];
    }
    
    //    NSLog(@"蓝色球数量 = %@",_BSource_Array);
}

-(NSMutableArray *)R_Array{
    if (!_R_Array) {
        _R_Array = [NSMutableArray arrayWithCapacity:6 ];
    }
    return _R_Array;
    
}

-(NSMutableArray *)B_Array{
    
    if (!_B_Array) {
        _B_Array = [NSMutableArray arrayWithCapacity:1 ];
    }
    
    return _B_Array;
}

-(void)RemoveArraySource{
    [self.B_Array removeAllObjects];
    [self.R_Array removeAllObjects];
}

// 摇一摇
-(void)shackButClick{
    [self RemoveArraySource];
    NSMutableArray  *array = [_SourceArray mutableCopy];
    for (int i = 0; i < _R_count_min; i ++ ) {
        
        if (i == _B_count_min || i == 0) {
            // 抽取篮球
            int index = arc4random()%_BSource_Array.count;
            //                    NSLog(@"篮球随机下标 = %d",index);
            int n = [array[index] intValue];
            [self.B_Array addObject:[NSString stringWithFormat:@"%d",n]];
            
            
        }else{
            
        }
        
        // 抽取红球
        int index = arc4random()%array.count;
        //        NSLog(@"红球随机下标 = %d",index);
        int n = [array[index] intValue];
        [self.R_Array addObject:[NSString stringWithFormat:@"%d",n]];
        [array removeObjectAtIndex:index];
        
        
        
    }
    [self.Dview.BallCollectionView reloadData];
    //     NSLog(@"%@",self.R_Array);
    
}

// 清空
-(void)clearButClick{
    [self RemoveArraySource];
    [self.Dview.BallCollectionView reloadData];
}

#pragma mark 返回多少个item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0) {
        return _R_count;
    }
    
    return _B_count;
    
}

// 返回的区数
-(NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}


-(NSMutableArray *)Data_Array{
    if (!_Data_Array) {
        _Data_Array = [NSMutableArray array];
    }
    return _Data_Array;
}

#pragma mark 返回item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        //   -----------红球
        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"coll_cell" forIndexPath:indexPath];
        
        cell.NubleLab.text =_SourceArray[indexPath.row];
        NSString *str = [NSString stringWithFormat:@"%lu",indexPath.row + 1];
        
        if ([self.R_Array containsObject:str]) {
            cell.NubleLab.backgroundColor = [UIColor redColor];
            cell.NubleLab.textColor = [UIColor whiteColor];
            
        }else{
            cell.NubleLab.backgroundColor = [UIColor whiteColor];
            cell.NubleLab.textColor = [UIColor blackColor];
        }
        
        return cell;
        
    }else{
        
        // ------------篮球
        BlueCollectionViewCell *B_cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"B_cell" forIndexPath:indexPath];
        B_cell.BlueleLab.text =_BSource_Array[indexPath.row];
        NSString *str = [NSString stringWithFormat:@"%lu",indexPath.row + 1];
        if ([self.B_Array containsObject:str]) {
            
            B_cell.BlueleLab.backgroundColor = Color(72, 156, 233, 1);
            B_cell.BlueleLab.textColor = [UIColor whiteColor];
            
        }else{
            B_cell.BlueleLab.backgroundColor = [UIColor whiteColor];
            B_cell.BlueleLab.textColor = [UIColor blackColor];
        }
        return B_cell;
    }
}





//选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        UICollectionViewCell *cell = [self.Dview.BallCollectionView cellForItemAtIndexPath:indexPath];
        
        CollectionViewCell *item = (CollectionViewCell *)cell;
        
        if ([item.NubleLab.backgroundColor isEqual:[UIColor redColor]]) {  // 已选中
            item.NubleLab.backgroundColor = [UIColor whiteColor];
            item.NubleLab.textColor = [UIColor blackColor];
            [_R_Array removeObject:item.NubleLab.text];
        }else{
            
            [_R_Array addObject:item.NubleLab.text];
            item.NubleLab.backgroundColor = [UIColor redColor];
            item.NubleLab.textColor = [UIColor whiteColor];
        }
        
    }else{
        UICollectionViewCell *cell = [self.Dview.BallCollectionView cellForItemAtIndexPath:indexPath];
        
        BlueCollectionViewCell *B_item = (BlueCollectionViewCell *)cell;
        
        if ([B_item.BlueleLab.backgroundColor isEqual:Color(72, 156, 233, 1)]) {
            // 已选中 改为 不选中
            [_B_Array removeObject:B_item.BlueleLab.text];
            B_item.BlueleLab.backgroundColor = [UIColor whiteColor];
            B_item.BlueleLab.textColor = [UIColor blackColor];
        }else{
            [_B_Array addObject:B_item.BlueleLab.text];
            B_item.BlueleLab.backgroundColor = Color(72, 156, 233, 1);
            B_item.BlueleLab.textColor = [UIColor whiteColor];
        }
        
    }
    
//    [MXUtility showTips:@"摇一摇机选一注"];
    
}
//取消选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
//    [MXUtility showTips:@"摇一摇机选一注"];
    //    NSLog(@"这个是取消选择状态");
    if (indexPath.section == 0) {
        UICollectionViewCell *cell = [self.Dview.BallCollectionView  cellForItemAtIndexPath:indexPath];
        CollectionViewCell *item = (CollectionViewCell *)cell;
        
        if ([item.NubleLab.backgroundColor isEqual:[UIColor whiteColor]]) {
            
             [_R_Array addObject:item.NubleLab.text];
            item.NubleLab.backgroundColor = [UIColor redColor];
            item.NubleLab.textColor = [UIColor whiteColor];
        }else{
            [_R_Array removeObject:item.NubleLab.text];
            item.NubleLab.backgroundColor = [UIColor whiteColor];
            item.NubleLab.textColor = [UIColor blackColor];
        }
        
        
    }else{
        
        UICollectionViewCell *cell = [self.Dview.BallCollectionView cellForItemAtIndexPath:indexPath];
        
        BlueCollectionViewCell *B_item = (BlueCollectionViewCell *)cell;
        
        if ([B_item.BlueleLab.backgroundColor isEqual:[UIColor whiteColor]]) {
            //
            [_B_Array addObject:B_item.BlueleLab.text];
            B_item.BlueleLab.backgroundColor = Color(72, 156, 233, 1);
            B_item.BlueleLab.textColor = [UIColor whiteColor];
        }else{
             [_B_Array removeObject:B_item.BlueleLab.text];
            B_item.BlueleLab.backgroundColor = [UIColor whiteColor];
            B_item.BlueleLab.textColor = [UIColor blackColor];
        }
        
    }
    
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView = nil;
    
    if (kind ==UICollectionElementKindSectionHeader) {
        
        MyHeaderView *headerV = (MyHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        //            headerV.button.hidden = NO;
        //            if (indexPath.section != 0) {
        //                headerV.button.hidden = YES;
        //            }
        //
        //            headerV.delegate = self;
        
        //@"请至少选择 1 个蓝球"
        headerV.textlabel2.text = _textArray[indexPath.section];
        reusableView = headerV;
        
    }
    
    return reusableView;
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size={ScreenWidth,30 * Heigt_P};
    //    if (section == 0) {
    //         CGSize size={0,0};
    //        return size;
    //    }
    return size;
}

//定义每个UICollectionView 的大小
- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath
{
    
    return CGSizeMake (Width_P * 45  , Heigt_P * 45);
    
}

//定义每个Section 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5 * Heigt_P, 15 * Heigt_P, 5 * Heigt_P+5, 15 * Heigt_P);//分别为上、左、下、右
}

@end
